<?php
/*
Plugin Name: WebApper
Plugin URI: http://rockstarwebdev.com/web-apper
Description: This plugin is a framework for rapid web app development using WordPress & Twitter Bootstrap.
Author: Jesse LaReaux
Version: 1.0
Author URI: http://rockstarwebdev.com/jesse-lareaux
*/

namespace WebApper;

// Include plugin files
	define( 'WEB_APPER_DIR_PATH', plugin_dir_path(__FILE__) );
	//Authorized.Net Settings
	define( 'AUTHORIZED_NET_URL', "https://test.authorize.net/gateway/transact.dll");
	define( 'AUTHORIZED_NET_LOGIN', '3N32MMywM' );
	define( 'AUTHORIZED_NET_TRAN_KEY', '65KT6kBwT3E8s8Cs' );
	
	require_once WEB_APPER_DIR_PATH . 'Base.php';
	require_once WEB_APPER_DIR_PATH . 'functions.php';
	require_once WEB_APPER_DIR_PATH . 'Shortcode.php';
	require_once WEB_APPER_DIR_PATH . 'MailLib.php';
	require_once WEB_APPER_DIR_PATH . 'PFBC/Form.php';
	foreach( glob ( WEB_APPER_DIR_PATH . 'Notification/*.php' ) as $filename ) {
		require_once( $filename );
	}
	foreach( glob ( WEB_APPER_DIR_PATH . 'Task/*.php' ) as $filename ) {
		require_once( $filename );
	}
	foreach( glob ( WEB_APPER_DIR_PATH . 'Service/*.php' ) as $filename ) {
		require_once( $filename );
	}
	foreach( glob ( WEB_APPER_DIR_PATH . 'Booking/*.php' ) as $filename ) {
		require_once( $filename );
	}
	foreach( glob ( WEB_APPER_DIR_PATH . 'Transaction/*.php' ) as $filename ) {
		require_once( $filename );
	}
	foreach( glob ( WEB_APPER_DIR_PATH . 'Shortcode/*.php' ) as $filename ) {
		require_once( $filename );
	}
	foreach(glob (WEB_APPER_DIR_PATH . 'Languages/*.php' ) as $filename ) {
		require_once( $filename );
	}
		
class WebApper extends Base {
	// Set plugin version
	private $db_version = 1.8;
	public $db_tables;

    public function __construct() {
		$this->populate();
        register_activation_hook( __FILE__, array($this, 'install') );
		add_action( 'plugins_loaded', array($this, 'update') );
        add_action( 'wp_enqueue_scripts', array($this, 'enqueue_scripts') );
    }

    /**
     * Create tables on plugin activation
     *
     * @global object $wpdb
	 * @param string $db_version
     * @since 1.0
     */
    public function install() {
		$installed_ver = get_option( 'webapper_db_version' );

		global $wpdb;
		if( $installed_ver !== $this->db_version ) {
			
			$fieldsSQL = "CREATE TABLE {$this->db_tables->fields} (
				ID SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
				field_name VARCHAR(100),
				field_id VARCHAR(55) NOT NULL,
				type VARCHAR(255) NOT NULL,
				required TINYINT(1) UNSIGNED NOT NULL,
				fieldset VARCHAR(100) DEFAULT '' NOT NULL,
				placeholder VARCHAR(150),
				short_desc VARCHAR(150),
				long_desc VARCHAR(255),
				options VARCHAR(255),
				default_value VARCHAR(150),
				validation ENUM('AlphaNumeric','RegExp'),
				regex VARCHAR(255),
				error_message VARCHAR(255),
				read_only TINYINT(1) UNSIGNED NOT NULL,
				show_in_table TINYINT(1) UNSIGNED NOT NULL,
				field_order SMALLINT UNSIGNED DEFAULT '0',
				UNIQUE INDEX (field_id),
				PRIMARY KEY (ID)
			);";
			
			$fieldSetsSQL = "CREATE TABLE {$this->db_tables->fieldSets} (
				ID SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
				fieldset_name VARCHAR(100),
				fieldset_id VARCHAR(55) NOT NULL,
				description VARCHAR(255),
				fieldset_order SMALLINT(6) UNSIGNED DEFAULT '0',
				UNIQUE INDEX (fieldset_id),
				PRIMARY KEY (ID)
			);";
			
			$tasksSQL = "CREATE TABLE {$this->db_tables->tasks} (
				ID SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
				subject VARCHAR(255) DEFAULT '' NOT NULL,
				due INT(10) UNSIGNED,
				charge_amount DECIMAL(10,2) DEFAULT '0.00' NOT NULL,
				task_type VARCHAR(55) DEFAULT 'generic' NOT NULL,
				author_id SMALLINT(6) UNSIGNED DEFAULT '0' NOT NULL,
				user_id SMALLINT(6) UNSIGNED NOT NULL,
				description VARCHAR(255) DEFAULT '' NOT NULL,
				status VARCHAR(55) NOT NULL,
				post_id SMALLINT UNSIGNED NOT NULL,
				created_on TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
				edited_on TIMESTAMP DEFAULT '0000-00-00 00:00:00',
				INDEX (user_id),
				PRIMARY KEY (ID)
			);";
			
			$notificationsSQL = "CREATE TABLE {$this->db_tables->notifications} (
				ID SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
				item_id SMALLINT(6) UNSIGNED DEFAULT '0' NOT NULL,
				type VARCHAR(55) NOT NULL,
				short_message VARCHAR(100) NOT NULL,
				long_message VARCHAR(255) NOT NULL,
				user_id SMALLINT(6) UNSIGNED NOT NULL,
				is_new TINYINT(1) UNSIGNED DEFAULT 1 NOT NULL,
				notified_on TIMESTAMP DEFAULT '0000-00-00 00:00:00',
				created_on TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
				INDEX (user_id),
				PRIMARY KEY (ID)
			);";

			$emailSQL = "CREATE TABLE {$this->db_tables->emails} (
				ID SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
				user_id SMALLINT(6) UNSIGNED NOT NULL,
				`to` VARCHAR(150) NULL,
				to_name VARCHAR(150) NULL,
				`from` VARCHAR(150) NULL,
				from_name VARCHAR(150) NULL,
				subject VARCHAR(255)  NULL,
				message_body TEXT NULL,
				attachment_path VARCHAR(255) NULL,
				tag VARCHAR(100) NULL,
				label VARCHAR(100) NULL,
				received_on TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
				date_on INT UNSIGNED NULL,
				INDEX (`to`),
				PRIMARY KEY (ID)
			);";
			
			$emailAttachmentSQL = "CREATE TABLE {$this->db_tables->email_attachments} (
				ID SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
				emails_id INT NOT NULL,
				file_path VARCHAR(150) NULL,
				file_name VARCHAR(150) NULL,				
				created_on TIMESTAMP DEFAULT CURRENT_TIMESTAMP,				
				INDEX (emails_id),
				PRIMARY KEY (ID)
			);";
			
			$emailLabelSQL ="CREATE TABLE {$this->db_tables->email_label} (
				ID SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
				user_id SMALLINT(6) UNSIGNED NOT NULL,
				label_name VARCHAR(200) NULL,								
				created_on TIMESTAMP DEFAULT CURRENT_TIMESTAMP,				
				INDEX (ID),
				PRIMARY KEY (ID)
			);";
			
			$serviceSQL ="CREATE TABLE {$this->db_tables->services} (
			service_id INT NOT NULL AUTO_INCREMENT, 
			service_name varchar(500) NULL,
			service_duration INT NULL,
			max_host INT NULL,
			max_seats INT,
            min_seats INT,
			fill_seats_before INT,
			confirm_seats_before INT,
            confirm_seats_within INT,
            autoconfirm INT,
			capability varchar(100) NULL,
            form_shortcode TEXT NULL,
			client_confirm_email TEXT NULL,
		    client_autoconfirm_email TEXT NULL,
            client_cancelation_email TEXT NULL,
            agent_confirm_email  TEXT NULL,
            agent_cancelation_email TEXT NULL,
            admin_confirm_email TEXT NULL,
            admin_cancelation_email TEXT NULL,
            creation_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
			status varchar(100) NULL,
            INDEX(service_id),
            PRIMARY KEY (service_id)
			);";
			
			$serviceScheduleSQL ="CREATE TABLE {$this->db_tables->schedules} (	
			id INT NOT NULL AUTO_INCREMENT,
			service_id INT,
			week_num  INT,
			startTime INT,
			endTime INT,
            creation_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
            INDEX(id),
            PRIMARY KEY (id)
			);";
			
			$reservationBookingSQL = "CREATE TABLE {$this->db_tables->booking}(
			booking_id int NOT NULL AUTO_INCREMENT Primary Key,
			service_id int NOT NULL,
			week_num int NOT NULL,
			reserveDate varchar(100) NOT NULL,
			reserveTime varchar(50)  NOT NULL,
			user_id int NOT NULL,
			postID int NOT NULL,
			creation_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
			Index(booking_id)	
			);";
			
		    $clientPayment_masterSQL="CREATE TABLE {$this->db_tables->user_payment_master} (
            user_payment_master_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
			user_id INT,
            client_name VARCHAR(100),
            co_client  VARCHAR(100),
            contracted_amount DECIMAL(18,2),
			interest_rate DECIMAL(18,2),
			start_date DATE,
			end_date DATE,	
			sales_rep_signed_up VARCHAR(100),
			consult_rep_assigned_to VARCHAR(100),
			card_holder_name VARCHAR(100),    
			card_type VARCHAR(100),           
			card_number VARCHAR(250),         
			card_expiration_month VARCHAR(50),
			card_expiration_year VARCHAR(50),
			status VARCHAR(100),
			card_country VARCHAR(100),             
			card_address VARCHAR(500),            
			card_city VARCHAR(100),                
			card_state VARCHAR(100),                
			card_ccv VARCHAR(250),           
			card_zip_code VARCHAR(250),
			bank_routing_number VARCHAR(250),
			bank_account_number VARCHAR(250),   
			bank_check_number VARCHAR(250), 
			principal_amount DECIMAL(18,2),
			trail_amount DECIMAL(18,2),
			trail_length VARCHAR(100),
			term_length VARCHAR(100),
			billing_frequency VARCHAR(100),
			billing_date DATE,
			INDEX(user_payment_master_id)   
           );";
		   $clientPayment_transactionSQL="CREATE TABLE {$this->db_tables->user_payment_transaction} (
            user_payment_transaction_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
			transaction_number VARCHAR(100),
			user_id INT,
			payment_for VARCHAR(100),
			received_as VARCHAR(100),
			amount_paid DECIMAL(18,2),
			transaction_date_time Timestamp,
			transaction_status VARCHAR(100),
			bank_name  VARCHAR(100),
			INDEX( user_payment_transaction_id )   
           );";
		   
		   
		   $reportingRep = "CREATE TABLE {$this->db_tables->reporting_rep} (
			reporting_rep_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
			newBiz_gross_sold decimal(18,2),
			newBiz_gross_collected decimal(18,2),
			pastBiz_gross_billed decimal(18,2),
			pastBiz_gross_collected decimal(18,2),
			pastBiz_defaulted_amount decimal(18,2),
			pastBiz_enrolled varchar(100),
			pastBiz_in_default varchar(100),
			recurring_membership varchar(50),
			INDEX( reporting_rep_id )
			  );";
			  
			  
						
			require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
			dbDelta( $fieldsSQL );
			dbDelta( $fieldSetsSQL );
			dbDelta( $tasksSQL );
			dbDelta( $notificationsSQL );
			dbDelta( $emailSQL );
			dbDelta( $emailAttachmentSQL );
			dbDelta( $emailLabelSQL );	
			dbDelta( $serviceSQL );		
			dbDelta( $serviceScheduleSQL );
			dbDelta( $reservationBookingSQL );
			dbDelta( $clientPayment_masterSQL );
			dbDelta( $clientPayment_transactionSQL );	
			dbDelta( $reportingRep );	
			update_option( 'webapper_db_version', $this->db_version );
		}
	}

    /**
     * Check db tables version on plugin upgrade
     *
     * @param string $db_version
     * @since 1.0
     */
	public function update() {
		if (get_site_option( 'webapper_db_version' ) != $this->db_version) {
			$this->install();
		}
	}
	
    /**
     * Enqueues Styles and Scripts
     *
     * @uses has_shortcode()
     * @since 1.0
     */
    public function enqueue_scripts() {
		// JS
		wp_enqueue_script( 'PFBC', plugins_url( 'assets/js/form.js' , __FILE__ ), 'jQuery', '1.0', true );
		wp_enqueue_script( 'DataTables', plugins_url( 'assets/js/dataTables.js' , __FILE__ ), 'jQuery', '1.9.4', true );
		wp_enqueue_script( 'DataTables-Bootstrap', plugins_url( 'assets/js/Bootstrap.js' , __FILE__ ), 'jQuery', '1.0', true );
		wp_enqueue_script( 'DataTables-Editor', plugins_url( 'assets/js/Editor.js' , __FILE__ ), 'jQuery', '1.0', true );
		wp_enqueue_script( 'DataTables-ColVis', plugins_url( 'assets/js/ColVis.js' , __FILE__ ), 'jQuery', '1.9.4', true );
		wp_enqueue_script( 'bootstrap-wysiwyg', plugins_url( 'assets/js/summernote.js' , __FILE__ ), 'jQuery', '1.0', true );
		wp_enqueue_script( 'bootstrap-multi-select', plugins_url( 'assets/js/magicsuggest.js' , __FILE__ ), 'jQuery', '1.3.1', true );
		wp_enqueue_script( 'bootstrap-select-box', plugins_url( 'assets/js/bootstrap-select.js' , __FILE__ ), 'jQuery', '1.4.2', true );
		wp_enqueue_script( 'bootstrap-select-min-box', plugins_url( 'assets/js/bootstrap-select.min.js' , __FILE__ ), 'jQuery', '1.4.2', true );	
		/* Calendar JS */
		
		wp_enqueue_script( 'bootstrap-ui-custom-min', plugins_url( 'assets/js/calendar/jquery-ui.custom.min.js' , __FILE__ ), 'jQuery', '1.10.3', true );
		wp_enqueue_script( 'bootstrap-calendar-min', plugins_url( 'assets/js/calendar/fullcalendar.min.js' , __FILE__ ), 'jQuery', '1.6.4', true );
		wp_enqueue_script( 'bootstrap-datepicker_js', plugins_url( 'assets/js/bootstrap-datepicker.js' , __FILE__ ), 'jQuery', '2.0', true );
		/*wp_enqueue_script( 'bootstrap-gCal', plugins_url( 'assets/js/calendar/gcal.js' , __FILE__ ), 'jQuery', '1.6.4', true );*/
						
		// CSS 
		wp_enqueue_script( 'bootstrap-multi-select', plugins_url( 'assets/js/magicsuggest.js' , __FILE__ ), 'jQuery', '1.3.1', true );
		wp_enqueue_script( 'bootstrap-select-box', plugins_url( 'assets/js/bootstrap-select.js' , __FILE__ ), 'jQuery', '1.4.2', true );
		wp_enqueue_script( 'bootstrap-select-min-box', plugins_url( 'assets/js/bootstrap-select.min.js' , __FILE__ ), 'jQuery', '1.4.2', true );	
		/* Calendar JS */
		
		wp_enqueue_script( 'bootstrap-ui-custom-min', plugins_url( 'assets/js/calendar/jquery-ui.custom.min.js' , __FILE__ ), 'jQuery', '1.10.3', true );
		wp_enqueue_script( 'bootstrap-calendar-min', plugins_url( 'assets/js/calendar/fullcalendar.min.js' , __FILE__ ), 'jQuery', '1.6.4', true );
		wp_enqueue_script( 'bootstrap-datepicker_js', plugins_url( 'assets/js/bootstrap-datepicker.js' , __FILE__ ), 'jQuery', '2.0', true );
		/*wp_enqueue_script( 'bootstrap-gCal', plugins_url( 'assets/js/calendar/gcal.js' , __FILE__ ), 'jQuery', '1.6.4', true );*/
		
		
		
				
				
		// CSS 
		wp_enqueue_style( 'Web-Apper', plugins_url( 'assets/css/webapper.css' , __FILE__ ), false, '1.0', 'all' );
		wp_enqueue_style( 'DataTables-Bootstrap', plugins_url( 'assets/css/dataTables.css' , __FILE__ ), false, '1.0', 'all' );
		wp_enqueue_style( 'DataTables-ColVis', plugins_url( 'assets/css/ColVis.css' , __FILE__ ), false, '1.0', 'all' );
		wp_enqueue_style( 'bootstrap-wysiwyg', plugins_url( 'assets/css/summernote.css' , __FILE__ ), false, '1.0', 'all' );
		wp_enqueue_style( 'font-awesome', plugins_url( 'assets/css/font-awesome.css' , __FILE__ ), false, '3.2.1', 'all' );	
		wp_enqueue_style( 'bootstrap-multi-select-css', plugins_url( 'assets/css/magicsuggest.css' , __FILE__ ), false, '1.3.1', 'all' );
		wp_enqueue_style( 'bootstrap-select-box-css', plugins_url( 'assets/css/bootstrap-select.css' , __FILE__ ), false, '1.4.2', 'all' );
		wp_enqueue_style( 'bootstrap-select-box-min-css', plugins_url( 'assets/css/bootstrap-select.min.css' , __FILE__ ), false, '1.4.2', 'all' ); 	
		/* Calendar CSS */
		wp_enqueue_style( 'bootstrap-calendar-min-css', plugins_url( 'assets/css/calendar/cupertino/jquery-ui.min.css' , __FILE__ ), false, '1.6.4', 'all' );
		wp_enqueue_style( 'bootstrap-fullcalendar-print', plugins_url( 'assets/css/calendar/fullcalendar.print.css' , __FILE__ ), false, '1.6.4', 'print' );
		wp_enqueue_style( 'bootstrap-fullcalendar', plugins_url( 'assets/css/calendar/fullcalendar.css' , __FILE__ ), false, '1.6.4', 'all' );
		/*wp_enqueue_style( 'bootstrap-datepicker', plugins_url( 'assets/css/bootstrap.css' , __FILE__ ), false, '1.4.0', 'all' );*/
    }
	/** Private Methods *******************************************************/
	/**
	 * Sets class properties
	 *
	 * @global wpdb $wpdb WordPress database object
	 */
	private function populate() {
		global $wpdb;
		$this->db_tables = (object) array(
			'fields' 					=> $wpdb->prefix . 'web_apper_fields',
			'fieldSets' 				=> $wpdb->prefix . 'web_apper_fieldsets',
			'tasks'						=> $wpdb->prefix . 'web_apper_tasks',
			'notifications' 			=> $wpdb->prefix . 'web_apper_notifications',
			'emails' 					=> $wpdb->prefix . 'web_apper_emails',
			'email_attachments' 		=> $wpdb->prefix . 'web_apper_attachments',
			'email_label' 				=> $wpdb->prefix . 'web_apper_email_label',
			'services' 					=> $wpdb->prefix . 'web_apper_bs_services',			
			'schedules' 				=> $wpdb->prefix . 'web_apper_bs_services_schedule',
			'booking' 					=> $wpdb->prefix . 'web_apper_reservation_booking',
			'user_payment_master' 		=> $wpdb->prefix . 'web_apper_user_payment_master',
			'user_payment_transaction' 	=> $wpdb->prefix . 'web_apper_user_payment_transaction',
			'reporting_rep' 			=> $wpdb->prefix . 'web_apper_bs_reporting_rep',
			'fields' 					=> $wpdb->prefix . 'web_apper_fields',
			'fieldSets' 				=> $wpdb->prefix . 'web_apper_fieldsets',
			'tasks'						=> $wpdb->prefix . 'web_apper_tasks',
			'notifications' 			=> $wpdb->prefix . 'web_apper_notifications',
			'emails' 					=> $wpdb->prefix . 'web_apper_emails',
			'email_attachments' 		=> $wpdb->prefix . 'web_apper_attachments',
			'email_label' 				=> $wpdb->prefix . 'web_apper_email_label',
			'services' 					=> $wpdb->prefix .'web_apper_bs_services',			
			'schedules' 				=> $wpdb->prefix .'web_apper_bs_services_schedule',
			'booking' 					=> $wpdb->prefix . 'web_apper_reservation_booking',
			'user_payment_master' 		=> $wpdb->prefix .'web_apper_user_payment_master',
			'user_payment_transaction' 	=> $wpdb->prefix .'web_apper_user_payment_transaction',
			'reporting_rep' 			=> $wpdb->prefix .'web_apper_bs_reporting_rep'
		);
	}

}

// Initialise plugin class & declare as global
$initialize = new WebApper(); 
global $webapper;
$webapper = (object) array( 'tables' => $initialize->db_tables );

?>